package cn.iocoder.yudao.module.member.point.mq;

import cn.hutool.extra.spring.SpringUtil;
import cn.iocoder.yudao.framework.common.exception.Assertions;
import cn.iocoder.yudao.module.api.infrastructure.config.ConfigApiV2;
import cn.iocoder.yudao.module.api.infrastructure.config.dto.MemberConfigRespDTO;
import cn.iocoder.yudao.module.api.member.point.enums.MemberPointBizTypeEnum;
import cn.iocoder.yudao.module.api.member.vip.message.VipOrderPaidMessage;
import cn.iocoder.yudao.module.api.member.vip.vo.VipOrderVO;
import cn.iocoder.yudao.module.api.shop.info.ShopInfoApi;
import cn.iocoder.yudao.module.api.trade.order.dto.TradeOrderRespDTO;
import cn.iocoder.yudao.module.api.trade.order.message.TradeOrderCompletedMessage;
import cn.iocoder.yudao.module.api.trade.scan.dto.ScanOrderDTO;
import cn.iocoder.yudao.module.api.trade.scan.message.ScanOrderPaidMessage;
import cn.iocoder.yudao.module.member.point.service.MemberPointRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import static cn.iocoder.yudao.module.api.infrastructure.ConfigConstants.CONFIG_MEMBER;

/**
 * <pre>
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 *  这里是文件说明
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * </pre>
 *
 * @author 山野羡民（1032694760@qq.com）
 * @since 2025/05/09
 */
@Component
@Slf4j
public class MemberPointConsumer {

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(VipOrderPaidMessage message) {
        log.info("[member/point][onMessage][会员缴费完成事件({})]", message);
        VipOrderVO order = message.getOrder();
        Assertions.notNull(order, "会员缴费完成事件携带的订单信息为空");
        SpringUtil.getBean(MemberPointRecordService.class).createPointRecord(order.getUserId(), order.getGivePoint(), MemberPointBizTypeEnum.BUY_VIP_GIVE, order.getTradeNo());
    }

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(TradeOrderCompletedMessage message) {
        log.info("[member/point][onMessage][交易订单完成事件({})]", message);
        TradeOrderRespDTO order = message.getOrder();
        Assertions.notNull(order, "交易订单完成事件携带的订单信息为空");
        // 打折部分返商家一定的积分
        MemberConfigRespDTO memberConfigRespDTO = SpringUtil.getBean(ConfigApiV2.class).getConfig(CONFIG_MEMBER, MemberConfigRespDTO.class);
        double discountYuan = (1d * order.getTotalPrice() - order.getPayPrice()) / 100d;
        int givePoint = (int) (discountYuan * memberConfigRespDTO.getPointTradeGiveUnitPointForSeller() / 100d);
        Long sellerUserId = SpringUtil.getBean(ShopInfoApi.class).getOwnerMemberUserId(order.getShopId());
        SpringUtil.getBean(MemberPointRecordService.class).createPointRecord(sellerUserId, givePoint, MemberPointBizTypeEnum.SOLD_PRODUCT_GIVE, order.getNo());
    }

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(ScanOrderPaidMessage message) {
        ScanOrderDTO order = message.getOrder();
        log.info("[member/point][onMessage][扫码订单付款完成事件({})]", order.getTradeNo());
        // 打折部分返商家一定的积分
        MemberConfigRespDTO memberConfigRespDTO = SpringUtil.getBean(ConfigApiV2.class).getConfig(CONFIG_MEMBER, MemberConfigRespDTO.class);
        double discountYuan = (1d * order.getOriginPrice() - order.getPrice()) / 100d;
        int givePoint = (int) (discountYuan * memberConfigRespDTO.getPointTradeGiveUnitPointForSeller() / 100d);
        Long sellerUserId = SpringUtil.getBean(ShopInfoApi.class).getOwnerMemberUserId(order.getShopId());
        SpringUtil.getBean(MemberPointRecordService.class).createPointRecord(sellerUserId, givePoint, MemberPointBizTypeEnum.QRCODE_SCAN_GIVE, order.getTradeNo());
    }

}
